package com.fumin.Filtter;


import com.fumin.util.StringUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Lingo on 2018/3/12.
 */

public class AuthCheckInterceptors implements HandlerInterceptor {

    private static String secrete = "WOCAONIMAGEBIgannilao123";
    private static String salt = "xxx";


    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        String user = httpServletRequest.getParameter("user");
        String time = httpServletRequest.getParameter("time");
        String auth = httpServletRequest.getParameter("auth");
        if (StringUtil.isNull(user) || StringUtil.isNull(time)){
            throw  new BussinessException("-1","无效的请求");
        }
        String tocken = StringUtil.MD5(StringUtil.MD5(user+time+secrete)+salt);
        if (!tocken.equals(auth)){
            throw new BussinessException("-1","无效的请求");
        }else {
            return true;
        }
//        String user = httpServletRequest.getHeader("User");
//        String timeStamp = httpServletRequest.getHeader("Time");
//        if (StringUtil.isNull(user) || StringUtil.isNull(timeStamp)){
//            throw  new BussinessException("-1","无效的请求");
//        }
//        String tocken = StringUtil.MD5(StringUtil.MD5(user+timeStamp+secrete)+salt);
//        if (tocken.equals(httpServletRequest.getHeader("Auth"))){
//            return true;
//        }else {
//          throw new BussinessException("-1","无效的请求");
//        }
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
